package controllers

import (
	"fly/helper"
	"fly/models"
	"fly/req"
	"fly/resp"
	"fly/schema"
	"fly/service"
	"github.com/beego/beego/v2/client/orm"
	"github.com/duke-git/lancet/v2/condition"
	"github.com/duke-git/lancet/v2/convertor"
	"net/url"
)

// AircraftLocationController 机位
type AircraftLocationController struct {
	BaseController
	SysUserService service.SysUserService
}

// Save ...
// @Title 添加或者编辑机位
// @Description 添加或者编辑机位
// @Param token header  string	false "授权Token"
// @Param body body req.AircraftLocationSaveReq true "添加或者编辑机位"
// @Success 200 {object} resp.AircraftLocationResp
// @router /save [post]
func (c *AircraftLocationController) Save() {
	con := req.AircraftLocationSaveReq{}
	c.CheckReqErrorAbort(c.BindJSON(&con))
	c.ValidReqParams(&con)
	aircraftLocation := models.AircraftLocation{}
	c.CheckRespErrorAbort(helper.CopyDeep(&aircraftLocation, con))
	var err error
	id := con.Id
	uid := c.GetReqUserId()
	if con.Id > 0 {
		old, _ := models.GetAircraftLocationById(id)
		c.CheckReqErrorAbort(helper.RequireTrue(old != nil, "未找到资源"))
		_ = helper.CopyOut(old, con)
		err = models.UpdateAircraftLocationById(old, uid)
	} else {
		id, err = models.AddAircraftLocation(&aircraftLocation, uid)
	}
	c.CheckRespErrorAbort(err)
	r := resp.AircraftLocationResp{}
	location, err := models.GetAircraftLocationById(id)
	err = resp.Copy2TimeResp(&r, location)
	c.CheckRespErrorAbort(err)
	r.CreatorName, err = c.SysUserService.GetUserRealNameById(uid)
	c.CheckRespErrorAbort(err)
	c.Success(r)
}

// Del ...
// @Title 删除机位
// @Description 删除机位
// @Param token header  string	false "授权Token"
// @Param path id int true "机位id"
// @Success 200 {int} "id"
// @router /:id [delete]
func (c *AircraftLocationController) Del() {
	id, err := convertor.ToInt(c.Ctx.Input.Param(":id"))
	c.CheckReqErrorAbort(err)
	c.CheckReqErrorAbort(helper.RequireTrue(c.SysUserService.IsSuperAdminUser(c.GetReqUserId()), "超级管理员才能操作"))
	aircraftCount, _ := orm.NewOrm().QueryTable(new(models.Aircraft)).Filter("location_id", id).Count()
	c.CheckReqErrorAbort(helper.RequireTrue(aircraftCount == 0, "该机位还有飞机关联使用"))
	c.CheckRespErrorAbort(models.DeleteAircraftLocation(id))
	c.Success(id)
}

// Enable 停用启用
// @Title Enable
// @Description 停用启用
// @Param token header  string	false "授权Token"
// @Param path id int true "字典id"
// @Success 200 {int} "id"
// @router /enable/:id [put]
func (c *AircraftLocationController) Enable() {
	id, err := convertor.ToInt(c.Ctx.Input.Param(":id"))
	c.CheckReqErrorAbort(err)
	m, err := models.GetAircraftLocationById(id)
	c.CheckAssertAbort(m, "资源不存在")
	m.Status = condition.TernaryOperator(m.Status == schema.Disable, schema.Enabled, schema.Disable)
	c.CheckRespErrorAbort(models.UpdateAircraftLocationById(m, c.GetReqUserId()))
	c.Ok()
}

// List ...
// @Title 所有机位列表
// @Description 所有机位列表-用于首页显示
// @Param token header  string	false "授权Token"
// @Param type query  int false "机位类型"
// @Param status query  int false "机位状态"
// @Success 200 {object} []resp.AircraftLocationResp
// @router /list [get]
func (c *AircraftLocationController) List() {
	params, _ := url.ParseQuery(c.Ctx.Request.URL.RawQuery)
	qs := orm.NewOrm().QueryTable(new(models.AircraftLocation))
	if params.Has("type") {
		qs = qs.Filter("type", params.Get("type"))
	}
	if params.Has("status") {
		qs = qs.Filter("status", params.Get("status"))
	}
	var list []*models.AircraftLocation
	_, err := qs.All(&list)
	c.CheckRespErrorAbort(err)
	var locationResps []resp.AircraftLocationResp
	c.CheckRespErrorAbort(resp.Copy2TimeResp(&locationResps, &list))
	//for i, airport := range list {
	//	locationResps[i].CreatorName, _ = c.SysUserService.GetUserRealNameById(airport.Creator)
	//	locationResps[i].UpdaterName, _ = c.SysUserService.GetUserRealNameById(airport.Updater)
	//}
	c.Success(list)
}
